package com.songcha.library_common.util

import java.lang.StringBuilder
import java.math.BigDecimal
import java.security.SecureRandom
import java.text.DecimalFormat
import kotlin.random.Random

class  StringUtil {
    companion object{
        @JvmStatic
        fun formatFloatString(f:Float):String{
            var str=f.toString()
            while (str.length>1){
                if (str.endsWith('0')) {
                    str = str.substring(0, str.length - 1);
                } else if (str.endsWith('.')) {
                    str = str.substring(0, str.length - 1);
                    break;
                } else {
                    break;
                }
            }
            return str
        }

        /***
         * exclude the zero behind decimal
         */
        @JvmStatic
        fun float2PlainString(num:Float):String{
           return float2PlainString(num.toDouble())
        }


        @JvmStatic
        fun float2PlainString(num:Double,format:String="0.00",isErasePercentile:Boolean=false):String{
            if(num==0.0) return "0"
            var tmp=num
            if(isErasePercentile && tmp>=0.1f){
                tmp=((num*10).toInt()/10f).toDouble()
            }
            val decimalFormat = DecimalFormat("#"+format)
            val str: String = decimalFormat.format(tmp)
            val bd = BigDecimal(str)
            return bd.stripTrailingZeros().toPlainString()
        }

        @JvmStatic
        fun hidePhoneMiddleNumber(phone:String):String{
            if(phone.length<11)
                return phone
            return phone.substring(0,3)+"****"+phone.substring(7,phone.length)
        }

        @JvmStatic
        fun printAliboRandomStringByCharArray(){
            var charArr= charArrayOf('ء','ي','ه','و','ا','ن','ل','م','ك','ف','ق','ل','غ','ع','ظ','ط',
                'ص','ش','س','ز','ض','ر','ذ','ر','خ','د','ج','ث','ت','ب','ا','ح')
            val maxLine=1000
            val maxCount=15
            var sr= SecureRandom()
            val dictionary= mutableListOf<String>()
            for(i in 0 until maxLine){
                var randCount= sr.nextInt(maxCount)+1
                var sb=StringBuilder()
                for(j in 0 until randCount){
                    var rand=sr.nextInt(charArr.size)
                    var char= charArr[rand]
                    sb.append(char)
                }
                val str=sb.toString()
                if(!dictionary.contains(str)){
                    dictionary.add(str)
                    LogUtil.log("printRandomStringByCharArray",str+"\n")
                }

            }

        }

        @JvmStatic
        fun getStringMiddle(str:String,start:String,end:String):String{
            if(str.isEmpty()) return ""
            if(start.isEmpty() && end.isEmpty()) return str
            val startIndex = if(start.isNotEmpty()) str.indexOf(start)+start.length else 0//str.indexOf(start)+start.length
            val endIndex = if(end.isNotEmpty()) str.indexOf(end,startIndex) else str.length
            if(endIndex<=startIndex) return ""
            return str.substring(startIndex,endIndex)
        }

        @JvmStatic
        fun completeZero(num:Int):String{
            if(num<10) return "0"+num
            else return num.toString()
        }

    }


}